package com.inforich.company.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.inforich.carManage.domain.Driver;
import com.inforich.carManage.service.IDriverService;
import com.inforich.common.core.domain.AjaxResult;
import com.inforich.orderManage.domain.Booking;
import com.inforich.orderManage.service.IBookingService;
import com.inforich.projectManage.domain.ProjectRubbishExpand;
import com.inforich.projectManage.domain.ProjectRubbishRemoval;
import com.inforich.projectManage.service.IProjectRubbishExpandService;
import com.inforich.projectManage.service.IProjectRubbishRemovalService;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.inforich.company.mapper.TransportCompanyMapper;
import com.inforich.company.domain.TransportCompany;
import com.inforich.company.service.ITransportCompanyService;

/**
 * 清运公司Service业务层处理
 *
 * @author hjm
 * @date 2022-10-23
 */
@Service
public class TransportCompanyServiceImpl extends
    ServiceImpl<TransportCompanyMapper, TransportCompany> implements ITransportCompanyService {

  @Autowired
  private IProjectRubbishExpandService projectRubbishExpandService;
  @Autowired
  private IProjectRubbishRemovalService projectRubbishRemovalService;
  @Autowired
  private IBookingService bookingService;
  @Autowired
  private IDriverService driverService;

  @Override
  public Page<TransportCompany> pageTransportCompany(Page<TransportCompany> page,
      Map<String, Object> map) {
    return baseMapper.pageTransportCompany(page, map);
  }

  @Override
  public List<TransportCompany> listTransportCompany(Map<String, Object> map) {
    return baseMapper.listTransportCompany(map);
  }

  @Override
  public TransportCompany getTransportCompanyById(String id) {
    return baseMapper.selectTransportCompanyById(id);
  }

  @Override
  public int insertTransportCompany(TransportCompany transportCompany) {
    return baseMapper.insertTransportCompany(transportCompany);
  }

  @Override
  public int updateTransportCompany(TransportCompany transportCompany) {
    return baseMapper.updateTransportCompany(transportCompany);
  }

  @Override
  public String checkData(TransportCompany transportCompany) {
    String id = transportCompany.getId();
    // 判断公司名称是否有重复
    int countName = this.count(new LambdaQueryWrapper<TransportCompany>()
        .eq(TransportCompany::getName, transportCompany.getName())
        .ne(StrUtil.isNotBlank(id), TransportCompany::getId, id));
    if (countName > 0) {
      return "公司名称重复";
    }
    // 判断统一社会信用码是否重复
    int countCode = this.count(new LambdaQueryWrapper<TransportCompany>()
        .eq(TransportCompany::getSocialCreditCode, transportCompany.getSocialCreditCode())
        .ne(StrUtil.isNotBlank(id), TransportCompany::getId, id));
    if (countCode > 0) {
      return "统一社会信用码重复";
    }
    return null;
  }

  @Override
  public Boolean checkBind(String companyId) {
    int count = projectRubbishExpandService.count(new LambdaQueryWrapper<ProjectRubbishExpand>()
        .eq(ProjectRubbishExpand::getTransportCompanyId, companyId));
    if (count > 0) {
      return true;
    }
    count = projectRubbishRemovalService.count(new LambdaQueryWrapper<ProjectRubbishRemoval>()
        .eq(ProjectRubbishRemoval::getTransportCompanyId, companyId));
    return count > 0;
  }

  @Override
  public AjaxResult removeTransportCompany(String id) {
    int count = bookingService.count(new LambdaQueryWrapper<Booking>()
        .eq(Booking::getTransportCompanyId, id));
    if (count > 0) {
      return AjaxResult.error("该公司已有预约单，不可再删除");
    }
    int driverCount = driverService.count(new LambdaQueryWrapper<Driver>()
        .eq(Driver::getTransportCompanyId, id));
    if (driverCount > 0) {
      return AjaxResult.error("该公司旗下有司机，不可删除");
    }
    removeById(id);
    return AjaxResult.success();
  }


}
